package com.tongyi.lizhishop.managers;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.Message;
import android.util.Log;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.Utils;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.download.DownloadUtils;
import com.tongyi.lizhishop.download.JsDownloadListener;
import com.tongyi.lizhishop.utils.PromptDialogUtils;

import java.io.File;
import java.io.InputStream;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.leefeng.promptlibrary.PromptDialog;
import retrofit2.Retrofit;

/**
 * Created by 梅俊 on 2018/5/11.
 */

public class APPUpdateManager {

    static String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "lizhi-seller.apk";

    private Context context;

    public APPUpdateManager(Context context) {
        this.context = context;
    }

    public void checkVersion() {
        final PromptDialog prompDialog = PromptDialogUtils.getPrompDialog((Activity) context);
        prompDialog.showLoading("请等待");
        String number = DeviceUtils.getAndroidID();
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = null;
        try {
            pi = pm.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String versionName = pi.versionName;
        int versioncode = pi.versionCode;
        String type = "2";
        APIServiceManager.getInstance().create(MessageApi.class)
                .checkAPP(number, "" + versionName, type).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        prompDialog.dismiss();
                        if ("succ".equals(value.getRe())) {
                            new AlertDialog.Builder(context).setTitle("有新的版本,是否升级")
                                    .setNegativeButton("算了", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    }).setPositiveButton("好的", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    downloadAPK();
                                }
                            }).show();
                        } else {
                            File file = new File(apkPath);
                            if (file.exists()) {
                                file.delete();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        prompDialog.dismiss();

                    }
                });

    }

    private void downloadAPK() {

        File file = new File(apkPath);
        if (file.exists()) {
            installApk();
        } else {
            final PromptDialog prompDialog = PromptDialogUtils.getPrompDialog((Activity) context);
            prompDialog
                    .showLoading("请等待");

            DownloadUtils downloadUtils = new DownloadUtils(new JsDownloadListener() {
                @Override
                public void onStartDownload() {

                }

                @Override
                public void onProgress(int progress) {

                }

                @Override
                public void onFinishDownload() {
                    prompDialog.dismiss();
                    installApk();
                }

                @Override
                public void onFail(String errorInfo) {

                }
            });


//        http://app.lizhigongying.com/apk/lizhi-seller.apk
            downloadUtils.download("/apk/lizhi-seller.apk", apkPath, new CommonObserver<InputStream>() {
                @Override
                public void onSuccess(InputStream value) {

                }

                @Override
                public void onFailure(Throwable e) {

                }
            });
        }
    }

    private void installApk() {
        IntentUtils.getInstallAppIntent(apkPath, "com.tongyi.lizhishop.fileprovider");
    }
}
